package DataStorage;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;


import HibernateSQLite.java.model.Person;

public class PersonDAOHibernate extends
		GenericDAOHibernate<Person, Long, IPersonDAO> implements IPersonDAO {

	@SuppressWarnings("unchecked")
	public boolean isExist(long person_id) {
		try {
			List<Person> list = this.getSession().createCriteria(Person.class)
					.add(Restrictions.eq("person_id", person_id)).list();
			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isExist(Person person) {
		try {
			List<Person> list = this.getSession().createCriteria(Person.class)
					.list();
			if (list.contains(person)) {
				return true;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isExist(String person_name) {
		try {
			List<Person> list = this.getSession().createCriteria(Person.class)
					.add(Restrictions.eq("person_name", person_name)).list();
			if (!list.isEmpty()) {
				return true;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isExist(String person_name, String person_password) {
		try {
			List<Person> list = this.getSession().createCriteria(Person.class)
					.add(Restrictions.eq("person_name", person_name))
					.add(Restrictions.eq("person_password", person_password))
					.list();
			if (!list.isEmpty()) {
				return true;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public Person getPerson(long person_id) {
		try {
			List<Person> list = getSession().createCriteria(Person.class)
					.add(Restrictions.eq("person_id", person_id)).list();

			for (Person p : list) {
				return p;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Person getPerson(String person_name) {
		try {
			List<Person> list = getSession().createCriteria(Person.class)
					.add(Restrictions.eq("person_name", person_name)).list();

			for (Person p : list) {
				return p;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Person getPerson(String person_name, String person_password) {
		try {
			List<Person> list = getSession().createCriteria(Person.class)
					.add(Restrictions.eq("person_name", person_name))
					.add(Restrictions.eq("person_password", person_password))
					.list();

			for (Person p : list) {
				return p;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Person> getListPerson() {
		try {
			List<Person> list = getSession().createCriteria(Person.class)
					.list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	public void savePerson(String person_name, String person_password) {
		try{
			Person person = null;
			if (!this.isExist(person_name)) {

				person = new Person();
				person.setPerson_name(person_name);
				person.setPerson_password(person_password);
				getSession().save(person);
			}
			this.commit();
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
	}
	
	public boolean verifyPassWord(String person_name,String verify_person_password){
		try{
			if(isExist(person_name)){
				Person person = this.getPerson(person_name);
				if(person.getPerson_password().equals(verify_person_password)){
					return true;
				}else{
					return false;
				}
			}
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}
	public void modifyPassWord(String person_name,String old_person_password, String new_person_password){
		try{
			if(verifyPassWord(person_name, old_person_password)){
				long person_id = this.getPerson(person_name,old_person_password).getPerson_id();
				Person person = (Person) getSession().load(Person.class, person_id);
				person.setPerson_password(new_person_password);
				this.saveOrUpdate(person);
			}else{
				System.out.println("old_person_password error");
			}
			
		}catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
	}

	public static void main(String[] args) {
		PersonDAOHibernate personDAO = new PersonDAOHibernate();
		String person_name = "superuser";
		String person_password = "superuser";
		personDAO.savePerson(person_name, person_password);
		/*
		boolean isexist = personDAO.verifyPassWord(person_name, person_password);
		System.out.println(isexist);
		String old_person_password = "superuser2";
		String new_person_password="superuser3";*/
		//personDAO.modifyPassWord("superuser", "superuser", "azerty");
		
	}

}
